#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <assert.h>

#if 0
int main()
{
	int a = 10000;
	FILE* pf = fopen("test.txt", "wb");
    assert(pf);
	fwrite(&a, 4, 1, pf);
	
	fclose(pf);
	pf = NULL;

	return 0;
}
#endif

#if 0
int main()
{
	//1.打开文件
	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)
	{
		perror(pf);
		return 1;
	}

	//2.写文件
	fputs("Are you ok?", pf);
	fputs("I am a student", pf);

	//3.关闭文件
	fclose(pf);
	pf = NULL;

	//4.打开文件
	pf = fopen("test.txt", "r");

	//5.读文件
	char arr[20] = "**********";
	fgets(arr, 10, pf);
	printf("%s\n", arr);

	//6.关闭文件
	fclose(pf);
	pf = NULL;

	return 0;
}
#endif

#if 0

int main()
{
	FILE* pf = fopen("test001.txt", "w");
	if (pf == NULL)
	{
		perror(pf);
		return 1;
	}
	fputs("This is an apple.", pf);
	fseek(pf, 9, SEEK_SET);
	int ret = ftell(pf);
	printf("%d\n", ret);	//9

	fputs("sam",pf);
	ret = ftell(pf);
	printf("%d\n", ret);	//12

	rewind(pf);
	ret = ftell(pf);
	printf("%d\n", ret);	//0

	fclose(pf);

	return 0;
}
#endif 


#if 0
int main()
{
	//1.open file - wirte
	FILE* pf = fopen("date.txt", "w");
	if (pf == NULL)
	{
		perror(pf);
		return 1;
	}

	//2.write file
	fputs("hello, world", pf);
	
	//3.close file
	fclose(pf);
	pf == NULL;

	//4.open file -read and write
	pf = fopen("date.txt", "r");
	
	FILE* pf2 = fopen("date_copy.txt", "w");
	assert(pf2 && pf);

	//5.copy
	char ch = 0;
	while (ch != EOF)
	{
		ch = fgetc(pf);
		fputc(ch, pf2);
	}

	//6.close file
	fclose(pf);
	pf = NULL;
	fclose(pf2);
	pf2 = NULL;

	return 0;
}
#endif 

#if 0
#include <stddef.h>
typedef struct STU
{
	int _age;
	char _name;
	double _score;
}STU;

int main()
{
	size_t age = offsetof(STU, _age);
	size_t name = offsetof(STU, _name);
	size_t score = offsetof(STU, _score);

	printf("%zd\n", age);
	printf("%zd\n", name);
	printf("%zd\n", score);

	return 0;
}
#endif

#if 1

#define SWAP(n)		\
do {				\
	if (n >= 0)		\
	{				\
		n <<= 1;	\
	}				\
	else			\
	{				\
		n <<= 1;	\
		n++;		\
	}				\
}while(0)

int main()
{
	int n = -1001;

	SWAP(n);

	int ret = n;

	printf("%d\n", ret);

	return 0;
}
#endif